package com.limao.CacheManager;

import javax.servlet.AsyncContext;
import javax.servlet.http.HttpServletResponse;

import com.limao.DBDataClass.CrowdCompanyDataClass;
import com.limao.Model.LzPluginException;
import com.limao.Model.PublicData;
import com.limao.Model.RecvData;
import com.limao.Tool.CacheClass;

public class CrowdCompanyInfo {

	//新鲜宝   开店
	public static Object SET_SHOP_OPERATION_NEW(HttpServletResponse response,AsyncContext asyncContext,int m,RecvData DataObj) throws LzPluginException
	{
		
		int userid=CacheClass.getIntValue(DataObj.userid);
		//是否已开店
		CrowdCompanyDataClass tmp=CrowdCompanyCache.getInstance().load_CompanyByUserid(userid);
		if(tmp!=null)
		{
			int state=CacheClass.getIntValue(tmp.state);
			if(state==0) //0禁用,1待审中,2审核未过，3正常
			{
				throw new LzPluginException("你已认证啦，目前禁用状态，请与客服联系。");
			}else
			if(state==1) //0禁用,1待审中,2审核未过，3正常
			{
				throw new LzPluginException("你的认证目前待审中，请耐心等待。");
			}else
			if(state==2) //0禁用,1待审中,2审核未过，3正常
			{
				throw new LzPluginException("你的认证目前审核未过，请与客服联系。");
			}else
			{
				throw new LzPluginException("你已认证啦，不需再认证；请直接登陆管理后台发起众筹操作。");
			}
		}
		
		//检验参数
		if(CacheClass.isEmpty(DataObj.companylogo) || CacheClass.isEmpty(DataObj.companyman)  
				|| CacheClass.isEmpty(DataObj.mancardimg0) || CacheClass.isEmpty(DataObj.mancardimg1) || CacheClass.isEmpty(DataObj.mancardno) ||
				 CacheClass.isEmpty(DataObj.address) || CacheClass.isEmpty(DataObj.tel) || CacheClass.isEmpty(DataObj.telman) )
		{
			throw new LzPluginException("内容为空或者参数不正确");
		}

		if(CacheClass.isEmpty(DataObj.iscompany))
			DataObj.iscompany="1";
		
		if("1".equalsIgnoreCase(DataObj.iscompany)) //公司
		{
			if(CacheClass.isEmpty(DataObj.companyname) || CacheClass.isEmpty(DataObj.companycardimg) || CacheClass.isEmpty(DataObj.companyno))
			{
				throw new LzPluginException("内容为空或者参数不正确");
			}
		}
		else//个人
		{
			DataObj.companyname=DataObj.companyman;
		}
		
		if(CacheClass.isEmpty(DataObj.paytype)) //0线下支付     1银联网上支付     
		{
			DataObj.paytype="0";
		}
		
		int paytype=CacheClass.getIntValue(DataObj.paytype);
		if(paytype==0) //0线下支付
		{
			if(CacheClass.isEmpty(DataObj.payimage))     
			{
				throw new LzPluginException("线下支付请提交支付凭证图片");
			}
		}
		
		if(!CacheClass.isEmpty(DataObj.introimg))
		{
			if(DataObj.introimg.endsWith(";"))
			{
				DataObj.introimg=DataObj.introimg.substring(0, DataObj.introimg.length()-1);
			}
		}
		
		//公司提交认证
		boolean flag=CrowdCompanyCache.getInstance().add_Company(userid+"",DataObj.companylogo,DataObj.iscompany,DataObj.companyname,DataObj.companyman,
				DataObj.mancardimg0,DataObj.mancardimg1,DataObj.companycardimg,DataObj.address,DataObj.tel,DataObj.telman,DataObj.intro,DataObj.introimg,DataObj.qq,DataObj.mancardno,DataObj.companyno);
		if(!flag)
		{
			throw new LzPluginException("操作出错sql。");
		}

		//取得刚才的公司id
		tmp=CrowdCompanyCache.getInstance().load_CompanyByUserid(userid);
		if(tmp==null)
		{
			throw new LzPluginException("提交资料失败。");
		}
		//取得认证费用系统参数
		float authcompayrmb=CacheClass.getFloatValue(BaseDataCacheManager.getInstance().Load_Param("authcompayrmb"));
		
		//生成认证支付单
		flag=CrowdCompanyCache.getInstance().add_CompanyPay(CacheClass.getIntValue(tmp.companyid),paytype,authcompayrmb,DataObj.payimage);
		
		if(!flag)
		{
			throw new LzPluginException("操作出错sql。");
		}
		
		PublicData data = new PublicData();
		data.state = "1";
		data.m = m+"";
		data.info = "提交成功，二工作日内工作人员会进行审核，期间请保持电话畅通。";
		
		return data;
	}		
		
}
